// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using SoapTrace.Core.Communication;
using System;
using System.Net.Sockets;

namespace SoapTrace.Tests.Utils
{
	/// <summary>
	/// <para>Represents a client connection in the <see cref="DerivedProxyServer"/>
	/// class. </para>
	/// <para><seealso cref="TcpSocketConnection"/></para>
	/// </summary>
	public class DerivedTcpSocketConnection : TcpSocketConnection
	{
		/// <summary>
		/// Client message received on this connection.
		/// </summary>
		private MockRemoteServerMessage m_ClientMessageReceived = 
			new MockRemoteServerMessage( );

		/// <summary>
		/// Server message received on this connection.
		/// </summary>
		private MockRemoteServerMessage m_ServerMessageReceived = 
			new MockRemoteServerMessage( );

		/// <summary>
		/// The server this connection belongs to.
		/// </summary>
		private DerivedProxyServer m_Server;

		/// <summary>
		/// Creates a new instance of the <see cref="DerivedTcpSocketConnection"/>
		/// class.
		/// </summary>
		/// <param name="socket">The accepted client socket.</param>
		/// <param name="server">The server that owns this connection.</param>
		public DerivedTcpSocketConnection( Socket socket, DerivedProxyServer server )
			: base( socket )
		{
			 m_Server = server;
		}

		/// <summary>
		/// Bytes received from the client.
		/// </summary>
		/// <param name="bytes">An array of bytes.</param>
		/// <param name="length">The number of bytes received.</param>
		protected override void BytesReceived( byte[ ] bytes, int length )
		{
			for( int i = 0; i < length; ++i )
			{
				if( m_ClientMessageReceived.AddByte( bytes[ i ] ) )
				{
					OnClientMessageArrived( );

					// In a real server this would
					// keep adding bytes and raising message received 
					// events, but since this is a test server we just
					// jump out of the loop.
					break; 
				}
			}
		}

		/// <summary>
		/// Bytes sent to the client.
		/// </summary>
		/// <param name="bytes">An array of bytes.</param>
		/// <param name="length">The number of bytes sent to the client.</param>
		protected override void BytesSent( byte[ ] bytes, int length )
		{
			for( int i = 0; i < length; ++i )
			{
				if( m_ServerMessageReceived.AddByte( bytes[ i ] ) )
				{
					OnServerMessageArrived( );

					// In a real server this would
					// keep adding bytes and raising message received 
					// events, but since this is a test server we just
					// jump out of the loop.
					break; 
				}
			}
		}
		/// <summary>
		/// Informs the server that a client message has been received.
		/// </summary>
		private void OnClientMessageArrived( )
		{
			m_Server.OnClientMessageReceived( m_ClientMessageReceived );
		}

		/// <summary>
		/// Informs the server that a remote server message has been received.
		/// </summary>
		private void OnServerMessageArrived( )
		{
			m_Server.OnServerMessageReceived( m_ServerMessageReceived );
		}
	}
}
